/*
 * Copyright (c) 2008-2010, Petr Panteleyev
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 *    1. Redistributions of source code must retain the above copyright notice,
 *       this list of conditions and the following disclaimer.
 *    2. Redistributions in binary form must reproduce the above copyright notice,
 *       this list of conditions and the following disclaimer in the documentation
 *       and/or other materials provided with the distribution.
 *    3. The name of the author may not be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package org.panteleyev.wordpress;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import org.panteleyev.blogclient.BloggerFrame;
import org.panteleyev.blogclient.ProfileDialog;
import org.panteleyev.blogclient.UserProfileManager;
import org.panteleyev.blogclient.UserProfileType;
import org.panteleyev.swing.OkCancelButtonPanel;
import org.panteleyev.swing.SwingFactory;

public class WPProfileDialog extends ProfileDialog {
    private WordPressUserProfile profile;

    // Controls
    private JTextField      profileNameEdit = new JTextField(20);
    private JTextField      userNameEdit = new JTextField(20);
    private JPasswordField  passwordEdit = new JPasswordField(20);
    private JTextField      xmlRpcEdit = new JTextField(20);
    private JLabel          xmlRpcLabel = new JLabel("XMLRPC:");
    private JCheckBox       storePasswordCheckBox;
    private JCheckBox       defaultProfileCheck = new JCheckBox(MAKEDEFAULT_CHECKBOX);

    /**
     * Creates new form GBProfileDialog
     * @param parent
     * @param prof
     */
    public WPProfileDialog(BloggerFrame parent, WordPressUserProfile prof) {
        super(parent);
        initComponents();
        
        profile = prof;

        profileNameEdit.setText(profile.getName());
        userNameEdit.setText(profile.getUserName());
        if (profile.getProfileType() == UserProfileType.WORDPRESSGEN) {
            xmlRpcEdit.setText(profile.getXmlRpc());
        } else {
            xmlRpcLabel.setVisible(false);
            xmlRpcEdit.setVisible(false);
        }

        boolean b = profile.getStorePasswd();
        storePasswordCheckBox.setSelected(b);
        passwordEdit.setEnabled(b);
        if (b) {
            passwordEdit.setText(profile.getHPasswd());
        } else {
            passwordEdit.setText("");
        }
        defaultProfileCheck.setSelected(profile.isDefaultProfile());

        pack();
    }

    private void initComponents() {
        setTitle(TITLE);
        getContentPane().setLayout(new BorderLayout());

        JLabel l1, l2, l3;
        JPanel panel = SwingFactory.createPanel(BorderFactory.createEmptyBorder(5, 5, 5, 5), SwingFactory.LAYOUT.BOX_Y,
            SwingFactory.createPanel(null, SwingFactory.LAYOUT.BOX_X,
                l1 = new JLabel(PROFILE_NAME_LABEL),
                Box.createHorizontalStrut(3),
                profileNameEdit
            ),
            Box.createVerticalStrut(3),
            SwingFactory.createPanel(null, SwingFactory.LAYOUT.BOX_X,
                l2 = new JLabel(USER_NAME_LABEL),
                Box.createHorizontalStrut(3),
                userNameEdit
            ),
            Box.createVerticalStrut(3),
            SwingFactory.createPanel(null, SwingFactory.LAYOUT.BOX_X,
                l3 = new JLabel(PASSWORD_LABEL),
                Box.createHorizontalStrut(3),
                passwordEdit
            ),
            Box.createVerticalStrut(3),
            SwingFactory.createPanel(null, SwingFactory.LAYOUT.BOX_X,
                xmlRpcLabel,
                Box.createHorizontalStrut(3),
                xmlRpcEdit
            ),
            Box.createVerticalStrut(3),
            storePasswordCheckBox = new JCheckBox(new AbstractAction(REMEMBER_PASSWORD_CHECKBOX) {
                @Override
                public void actionPerformed(ActionEvent e) {
                    passwordEdit.setEnabled(storePasswordCheckBox.isSelected());
                }
            }),
            Box.createVerticalStrut(3),
            defaultProfileCheck
        );

        SwingFactory.setCommonPreferredSize(l1, l2, l3, xmlRpcLabel);
        SwingFactory.setAlignmentX(panel, LEFT_ALIGNMENT);
        
        getContentPane().add(panel, BorderLayout.CENTER);
        getContentPane().add(new OkCancelButtonPanel(this), BorderLayout.SOUTH);
    }

    @Override
    public boolean showDialog() {
        boolean res = super.showDialog();
        if (res) {
            profile.setName(profileNameEdit.getText());
            profile.setUserName(userNameEdit.getText());
            if (profile.getProfileType() == UserProfileType.WORDPRESSGEN) {
                profile.setXmlRpc(xmlRpcEdit.getText());
            }
            if (storePasswordCheckBox.isSelected()) {
                String newPasswd = new String(passwordEdit.getPassword());
                if (!profile.getStorePasswd() || !newPasswd.equals(profile.getHPasswd())) {
                    profile.setPassword(newPasswd);
                }

                profile.setStorePasswd(true);
            } else {
                profile.setStorePasswd(false);
                profile.setPassword("");
            }
            profile.setDefaultProfile(defaultProfileCheck.isSelected());
        }
        return res;
    }

    @Override
    protected void onOk() {
        String msg = UserProfileManager.checkProfileName(profileNameEdit.getText(), profile);
        if (msg != null) {
            JOptionPane.showMessageDialog(getMainFrame(), msg, "Error", JOptionPane.ERROR_MESSAGE);
        } else {
            super.onOk();
        }
    }
}
